﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using IRemoting;
using System.Collections;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
using System.Configuration;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace QLNH_SERVER
{
    public partial class MainForm : Form
    {
        #region Properties

        private TcpListener tcpListener;
        private Manager manager;

        #endregion

        public MainForm()
        {
            InitializeComponent();
        }

        #region Control Events

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Get app settting
            int portNumber = Convert.ToInt32(ConfigurationManager.AppSettings["PortNumber"].ToString());
            int maxPendingConnection = Convert.ToInt32(ConfigurationManager.AppSettings["MaxPendingConnection"].ToString());

            // Set up tcp listener
            tcpListener = new TcpListener(IPAddress.Any, portNumber);
            tcpListener.Start(maxPendingConnection);

            // Set up manager
            manager = new Manager(this, tcpListener);
            manager.work();

            this.notifyIcon.BalloonTipText = "Máy chủ đã được khởi động";
            this.notifyIcon.ShowBalloonTip(3000);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            WindowState = FormWindowState.Minimized;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                Hide();
                notifyIcon.BalloonTipText = "Máy chủ đang chạy ở chế độ ẩn";
                notifyIcon.ShowBalloonTip(3000);
            }
        }

        private void bt_stop_Click(object sender, EventArgs e)
        {
            StopServer();
        }

        private void bt_restart_Click(object sender, EventArgs e)
        {
            RestartServer();
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                TrayMenu.Show();
            }
        }

        private void cm_restart_Click(object sender, EventArgs e)
        {
            bt_restart_Click(null, null);
        }

        private void cm_stop_Click(object sender, EventArgs e)
        {
            bt_stop_Click(null, null);
        }

        #endregion

        #region Functions

        private void RestartServer()
        {
            notifyIcon.BalloonTipText = "Đang khởi động lại máy chủ...";
            notifyIcon.ShowBalloonTip(2000);
            Thread.Sleep(2000);

            // Clear online user list
            manager.clearUsers();

            notifyIcon.BalloonTipText = "Máy chủ đã được khởi động lại";
            notifyIcon.ShowBalloonTip(2000);
        }

        private void StopServer()
        {
            notifyIcon.BalloonTipText = "Đang tắt máy chủ...";
            notifyIcon.ShowBalloonTip(2000);
            Thread.Sleep(2000);
            Dispose();
        }

        public void showTotalOnline(int total)
        {
            tb_status.BeginInvoke((MethodInvoker)(() => tb_status.Text = "Số tài khoản trực tuyến: " + total));
        }

        #endregion
    }    
}
